package com.jiarong.mapper;

import com.jiarong.domain.Order;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author 加绒
 * @date 2021/10/28 9:59
 */
public interface OrderMapper {
    /**
     * （一对一）查询所有订单
     * 第一种方式：多表查询
     *
     * @return 订单信息集合
     */
    @Select("select * from orders o, user u where o.user_id = u.user_id")
    @Results({
            @Result(id = true, column = "order_id", property = "orderId"),
            @Result(column = "order_time", property = "orderTime"),
            @Result(column = "total_Amount", property = "totalAmount"),
            @Result(id = true, column = "user_id", property = "user.userId"),
            @Result(column = "username", property = "user.username"),
            @Result(column = "password", property = "user.password"),
            @Result(column = "birthday", property = "user.birthday")
    })
    List<Order> selectAll01();

    /**
     * （一对一）查询所有订单
     * 第二种方式：子查询
     * javaType属性可以省略不写 MyBatis会根据类型处理器自动识别 写多错多
     *
     * @return 订单信息集合
     */
    @Select("select * from orders")
    @Results({
            @Result(id = true, column = "order_id", property = "orderId"),
            @Result(column = "order_time", property = "orderTime"),
            @Result(column = "total_Amount", property = "totalAmount"),
            @Result(id = true, column = "user_id",
                    property = "user", /*javaType = User.class,*/
                    one = @One(select = "com.jiarong.mapper.UserMapper.selectById"))
    })
    List<Order> selectAll02();

    /**
     * （一对一）查询所有订单
     * 第三种方式：连接查询
     *
     * @return 订单信息集合
     */
    @Select("select * from orders o inner join user u on u.user_id = o.user_id")
    @Results({
            @Result(id = true, column = "order_id", property = "orderId"),
            @Result(column = "order_time", property = "orderTime"),
            @Result(column = "total_Amount", property = "totalAmount"),
            @Result(id = true, column = "user_id", property = "user.userId"),
            @Result(column = "username", property = "user.username"),
            @Result(column = "password", property = "user.password"),
            @Result(column = "birthday", property = "user.birthday")
    })
    List<Order> selectAll03();

    /**
     * 根据用户id查询订单信息
     *
     * @param userId 用户id
     * @return 订单信息集合
     */
    @Select("select * from orders where user_id = #{userId}")
    @Results({
            @Result(id = true, column = "order_id", property = "orderId"),
            @Result(column = "order_time", property = "orderTime"),
            @Result(column = "total_Amount", property = "totalAmount")
    })
    List<Order> selectOrderByUserId(Integer userId);
}
